var async = require('async');
var bookingService = rootRequire('services/bookingService')
var hotelService = rootRequire('services/hotelService')
var moment  = require('moment')
var timeUtil = rootRequire('utils/time')
var loginUtil = rootRequire('utils/login')

module.exports =  function(server){
    server.route(
            {
                method:['GET','POST'],path:"/booking",
                handler:function(req,reply){
                    var hotelno = req.query['hn']
                    var payload  = req.payload;
                    var defaultDate =  timeUtil.getDefaultBeginEndFormat()
                    var startDate = defaultDate.startDate,
                        endDate = defaultDate.endDate;

                    if(req.method.toLowerCase()=='post'){
                        var inTime = payload.inTime,
                            outTime = payload.outTime,
                            inHour = payload.inHour,
                            outHour = payload.outHour;
                        startDate = inTime+' '+inHour+':00:00'
                        endDate = outTime+' '+outHour+':00:00'
                    }
                    //console.dir(req.payload)

                    async.parallel({
                        roomList:function(next){
                            bookingService.getReserveRoom({
                                hotelno:hotelno,
                                beginDate:startDate,
                                endDate:endDate
                            },function(err,body){
                                if(err)return next(err);
                                next(null,body['rtList'])
                            })
                        },
                        roomType:function(next){
                            hotelService.getRoomTypeList({
                                hotelno:hotelno,
                                rtCode:''
                            },function(err,body){
                                if(err) return next(err)
                                next(null,body['roomtypelist'])
                            })
                        },
                        hotel:function(next){
                            hotelService.getMainHotel(hotelno,function(err,body){

                                if(err)return next(err);
                                var info = body['hotel']
                                next(null,info)
                            })
                        },
                    }, function(err,results){
                        if(err){
                            //return reply.redirect('api_404?err='+err)
                            return reply(err)
                        }
                        var roomList = results.roomList,
                            roomType = results.roomType

                        var roomTypeMap = {}
                        roomType.forEach(function(item){
                            roomTypeMap[item.rtCode] = item
                        })

                        for(var i = 0;i<roomList.length;i++){
                            var room = roomList[i],
                                code = room['rtCode'];
                            Object.assign(roomList[i],roomTypeMap[code])
                        }

                        var hotel = results.hotel


                        var token = loginUtil.parseToken(req)
                        var phone = '',
                            linkMan=''
                        console.log(token)
                        if(!token.error){
                            phone = token.phone
                            linkMan = token.name || ''
                        }

                        reply.view("booking",{
                            title:'快速预订',
                            active:{booking:'active'},
                            hotelNo:hotelno,
                            hotelUrl:'/hotel?hn='+hotelno,
                            myUrl:'/my?hn='+hotelno,
                            roomList:roomList,
                            hotel:hotel,
                            startDate:startDate,
                            endDate:endDate,
                            phone:phone,
                            linkMan:linkMan
                        })
                    })
                }
            }

    )

    server.route({
        method:['POST'],path:'/submit-order',
        handler:function(req,reply){
            var payload = req.payload;
            var hotelNo = payload.hotelno
            var phone = payload.phoneNum
            var linkMan = payload.linkMan
            //console.log(payload)
            bookingService.submitOrder(payload,function(err,body){
                if(err){
                    return reply({
                        error:'订单生成失败',
                        reason:err
                    })
                }
                //{ code: '',
                //    hotelno: 'sdcs',
                //    info: '',
                //    needpay: '2',
                //    orderNum: 'RS1512060009002',
                //    times: '10' }

                //var token = loginUtil.parseToken(req)
                ////console.log(token)
                //var phone = 'unknown',
                //    isLogin = false;
                //if(!token.error){
                //    phone = token.phone
                //    phone = phone.substr(0,3)+'****'+phone.substr(7)
                //    isLogin = true
                //}
                //if(isLogin){
                var token = loginUtil.setJwt({hotelno:hotelNo,phone:phone,name:linkMan})
                reply(body).state('__j',token)
            })
        }
    })

    server.route({
        method:['POST'],path:'/cancel-order',
        handler:function(req,reply){
            var payload = req.payload
            //var orderNum = payload.orderNum
            bookingService.cancelOrder(payload,function(err,body){
                if(err){
                    return reply({
                        error:'订单取消失败',
                        reason:err
                    })
                }
                reply('cancel-success');
            })
        }
    })
}
